package com.ev.report.service.impl;

import com.ev.apis.model.DsResultResponse;
import com.ev.common.domain.PageModel;
import com.ev.custom.dao.DeviceCapacityDao;
import com.ev.custom.dao.DeviceDao;
import com.ev.custom.domain.*;
import com.ev.framework.config.Constant;
import com.ev.framework.config.ConstantForDevice;
import com.ev.framework.config.ConstantForMES;
import com.ev.framework.utils.*;
import com.ev.mes.dao.CrewSchedulingItemDao;
import com.ev.mes.dao.ShiftDao;
import com.ev.mes.domain.CrewSchedulingItemDO;
import com.ev.mes.domain.ShiftDO;
import com.ev.report.dao.DeviceAccountingReportDao;
import com.ev.report.dao.ManageKanbanServiceDao;
import com.ev.report.service.ManageKanbanService;
import com.ev.report.vo.AllDeviceManageVO;
import com.ev.report.vo.DevicePageParam;
import com.ev.report.vo.PointVO;
import com.ev.report.vo.ProcessReportMiniVO;
import com.ev.system.domain.DeptDO;
import com.ev.system.service.DeptService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ManageKanbanServiceImpl implements ManageKanbanService {
    @Autowired
    private ManageKanbanServiceDao kanbanService;
    @Autowired
    private DeviceDao deviceDao;
    @Autowired
    private DeptService deptService;
    @Autowired
    private DeviceCapacityDao deviceCapacityDao;
    @Autowired
    private ShiftDao shiftDao;
    @Autowired
    private CrewSchedulingItemDao crewSchedulingItemDao;
    @Autowired
    private DeviceAccountingReportDao reportDao;

    @Override
    public List<Map<String, Object>> feedingList() {
        return kanbanService.feedingList();
    }

    @Override
    public List<Map<String, Object>> getProductionStatistics(Map<String, Object> param) {
        return kanbanService.getProductionStatistics(param);
    }

    @Override
    public List<Map<String, Object>> badProductList(Map<String, Object> param) {
        return kanbanService.badProductList(param);
    }

    @Override
    public R deviceTask() {
        Map<String, Object> param = Maps.newHashMap();
        // 维修事件
        List<RepairEventDO> repairEventDOS = reportDao.repairEventList(param);
        // 巡检记录
        List<PatrolRecordDO> patrolRecordDOS = reportDao.patrolList(param);
        // 保养记录
        List<UpkeepRecordDO> upkeepRecordDOS = reportDao.upkeepList(param);

        // 维修完成数量 已验收
        long eventCompleteCount = repairEventDOS
                .stream()
                .filter(e -> ConstantForDevice.ALREADY_CHECK.equals(e.getStatus()))
                .count();

        // 巡检完成数量 已填写或者关闭的
        long patrolCompleteCount = patrolRecordDOS
                .stream()
                .filter(e -> ConstantForDevice.STATE_STOP_OVER.equals(e.getStatus()) || ConstantForDevice.CLOSE.equals(e.getStatus()))
                .count();

        // 保养完成数量 已验收或者关闭的
        long upkeepCompleteCount = upkeepRecordDOS
                .stream()
                .filter(e -> ConstantForDevice.ALREADY_CHECK.equals(e.getStatus()) || ConstantForDevice.CLOSE.equals(e.getStatus()))
                .count();

        Map<String, Object> result = Maps.newHashMap();
        result.put("eventCompleteCount", eventCompleteCount);
        result.put("patrolCompleteCount", patrolCompleteCount);
        result.put("upkeepCompleteCount", upkeepCompleteCount);
        int eventCount = repairEventDOS.size();
        result.put("eventCount", eventCount);
        int patrolCount = patrolRecordDOS.size();
        result.put("patrolCount", patrolCount);
        int upkeepCount = upkeepRecordDOS.size();
        result.put("upkeepCount", upkeepCount);
        result.put("eventRate", eventCount == 0 ? BigDecimal.ZERO : BigDecimal.valueOf(eventCompleteCount).divide(BigDecimal.valueOf(eventCount), Constant.BIGDECIMAL_ZERO, BigDecimal.ROUND_HALF_UP));
        result.put("patrolRate", patrolCount == 0 ? BigDecimal.ZERO : BigDecimal.valueOf(patrolCompleteCount).divide(BigDecimal.valueOf(patrolCount), Constant.BIGDECIMAL_ZERO, BigDecimal.ROUND_HALF_UP));
        result.put("upkeepRate", upkeepCount == 0 ? BigDecimal.ZERO : BigDecimal.valueOf(upkeepCompleteCount).divide(BigDecimal.valueOf(upkeepCount), Constant.BIGDECIMAL_ZERO, BigDecimal.ROUND_HALF_UP));

        return R.ok(result);
    }

    @Override
    public R deviceTaskOverview() {
//        超时定义 计划结束时间<现在时间
        Map<String, Object> result = Maps.newHashMap();
        Date now = new Date();
        Map<String, Object> param = Maps.newHashMap();
        // 维修事件
        List<RepairEventDO> repairEventDOS = reportDao.repairEventList(param);
        // 巡检记录
        List<PatrolRecordDO> patrolRecordDOS = reportDao.patrolList(param);
        // 保养记录
        List<UpkeepRecordDO> upkeepRecordDOS = reportDao.upkeepList(param);

        // 保养任务
        // 保养待处理的
        List<UpkeepRecordDO> upkeepWaitingDeal = upkeepRecordDOS
                .stream()
                .filter(e -> ConstantForDevice.WAITING_DEAL.equals(e.getResult()))
                .collect(Collectors.toList());
        int upkeepWaitingDealTimeOutCount = (int) upkeepWaitingDeal.stream().filter(e -> e.getEndTime() != null && e.getEndTime().before(now)).count();
        int upkeepWaitingDealCount = upkeepWaitingDeal.size() - upkeepWaitingDealTimeOutCount;

        List<UpkeepRecordDO> upkeepWaitingCheck = upkeepRecordDOS
                .stream()
                .filter(e -> ConstantForDevice.WAITING_CHECK.equals(e.getResult()))
                .collect(Collectors.toList());
        int upkeepWaitingCheckTimeOutCount = (int) upkeepWaitingCheck.stream().filter(e -> e.getEndTime() != null && e.getEndTime().before(now)).count();
        int upkeepWaitingCheckCount = upkeepWaitingCheck.size() - upkeepWaitingCheckTimeOutCount;
        int upkeepTimeOutCount = upkeepWaitingDealTimeOutCount + upkeepWaitingCheckTimeOutCount;
        Map<String, Integer> upkeep = Maps.newHashMap();
        upkeep.put("upkeepWaitingDealCount", upkeepWaitingDealCount);
        upkeep.put("upkeepWaitingCheckCount", upkeepWaitingCheckCount);
        upkeep.put("upkeepTimeOutCount", upkeepTimeOutCount);
        upkeep.put("totalCount", upkeepWaitingDealCount + upkeepWaitingCheckCount + upkeepTimeOutCount);
        result.put("upkeep", upkeep);
        // 维修任务
        // 维修待处理的
        List<RepairEventDO> repairWaitingDeal = repairEventDOS
                .stream()
                .filter(e -> ConstantForDevice.WAITING_DEAL.equals(e.getStatus()))
                .collect(Collectors.toList());
        int repairWaitingDealTimeOutCount = (int) repairWaitingDeal.stream().filter(e -> e.getPlanTime() != null && e.getPlanTime().before(now)).count();
        int repairWaitingDealCount = repairWaitingDeal.size() - repairWaitingDealTimeOutCount;

        List<RepairEventDO> repairWaitingCheck = repairEventDOS
                .stream()
                .filter(e -> ConstantForDevice.WAITING_CHECK.equals(e.getStatus()))
                .collect(Collectors.toList());
        int repairWaitingCheckTimeOutCount = (int) repairWaitingCheck.stream().filter(e -> e.getPlanTime() != null && e.getPlanTime().before(now)).count();
        int repairWaitingCheckCount = repairWaitingCheck.size() - repairWaitingCheckTimeOutCount;
        int repairTimeOutCount = repairWaitingDealTimeOutCount + repairWaitingCheckTimeOutCount;
        Map<String, Integer> repair = Maps.newHashMap();
        repair.put("repairWaitingDealCount", repairWaitingDealCount);
        repair.put("repairWaitingCheckCount", repairWaitingCheckCount);
        repair.put("repairTimeOutCount", repairTimeOutCount);
        repair.put("totalCount", repairWaitingDealCount + repairWaitingCheckCount + repairTimeOutCount);
        result.put("repair", repair);

        // 巡检任务
        // 巡检待处理的
        List<PatrolRecordDO> patrolWaitingDeal = patrolRecordDOS
                .stream()
                .filter(e -> ConstantForDevice.WAITING_DEAL.equals(e.getStatus()))
                .collect(Collectors.toList());
        int patrolTimeOutCount = (int) patrolWaitingDeal.stream().filter(e -> e.getEndTime() != null && e.getEndTime().before(now)).count();
        int patrolWaitingDealCount = patrolWaitingDeal.size() - patrolTimeOutCount;
        Map<String, Integer> patrol = Maps.newHashMap();
        patrol.put("patrolWaitingDealCount", patrolWaitingDealCount);
        patrol.put("patrolTimeOutCount", patrolTimeOutCount);
        patrol.put("totalCount", patrolWaitingDealCount + patrolTimeOutCount);
        result.put("patrol", patrol);
        // 总数量
        int totalCount = patrolWaitingDealCount + patrolTimeOutCount
                + repairWaitingDealCount + repairWaitingCheckCount + repairTimeOutCount
                + upkeepWaitingDealCount + upkeepWaitingCheckCount + upkeepTimeOutCount;
        result.put("totalCount", totalCount);
        return R.ok(result);
    }

    @Override
    public R outPut(PageModel pageParam) {
        // 车间
        Map<String, Object> param = Maps.newHashMap();
        param.put("type", Constant.WORK_SHOP);
        List<DeptDO> deptDOList = deptService.list(param);
        // 无车间
        if (deptDOList.size() == 0) {
            return R.ok();
        }
        List<Long> deptIdList = deptDOList.stream().map(DeptDO::getDeptId).collect(Collectors.toList());
        // 获取一年的数据
        Date beginDayOfYear = DatesUtil.getBeginDayOfYear();
        Date endDayOfYear = DatesUtil.getEndDayOfYear();
        // 取当年报工表中数据（合格数量 若无取检验单的合格数量）
        List<ProcessReportMiniVO> processReportInfo = kanbanService.getProcessReportInfo(beginDayOfYear, endDayOfYear, deptIdList);
        if (processReportInfo.size() == 0) {
            return R.ok();
        }
        Map<Long, String> deptMap = deptDOList.stream().collect(Collectors.toMap(DeptDO::getDeptId, DeptDO::getName));
        List<Triple<String, Integer, Integer>> monthDay = DateUtils.getMonthDayWithInt();
        List<String> yearDays = DateUtils.getYearDays();
        String thisMonth = DateUtils.getThisMonth();
        List<String> weekDays = DateUtils.getWeekDays();
        // 按部门按月分组
        Map<Long, Map<String, List<ProcessReportMiniVO>>> totalGroup = processReportInfo.stream()
                .collect(Collectors.groupingBy(ProcessReportMiniVO::getDeptId
                        , Collectors.groupingBy(e -> e.getReportDate().substring(0, 7))));

        List<LinkedHashMap<String, Object>> allResult = Lists.newArrayList();
        LinkedHashMap<String, Object> deptGroup;
        LinkedHashMap<String, Object> monthGroup;
        LinkedHashMap<String, Object> weekGroup;
        LinkedHashMap<String, Object> dayGroup;
        // 最外层 定义车间
        for (Long deptId : deptIdList) {
            deptGroup = Maps.newLinkedHashMap();
            deptGroup.put("deptId", deptId);
            deptGroup.put("deptName", deptMap.get(deptId));
            // 若该部门无数据则循环下一个部门
            Map<String, List<ProcessReportMiniVO>> timeTotalGroup = totalGroup.get(deptId);
            if (timeTotalGroup == null) {
                allResult.add(deptGroup);
                continue;
            }
            // 第二层 今年的月份
            monthGroup = Maps.newLinkedHashMap();
            for (String yearDay : yearDays) {
                List<ProcessReportMiniVO> processReportMiniVOS = timeTotalGroup.get(yearDay);
                // 若无该月份
                if (processReportMiniVOS == null) {
                    // XXX 这里的月份可以换成中文
                    // key 2020-01
                    monthGroup.put(yearDay, 0);
                    continue;
                }
                BigDecimal monthCount = processReportMiniVOS.stream().map(ProcessReportMiniVO::getConformityCount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
                monthGroup.put(yearDay, monthCount);
            }
            deptGroup.put("monthGroup", monthGroup);

            boolean monthIsExist = timeTotalGroup.containsKey(thisMonth);
            weekGroup = Maps.newLinkedHashMap();
            // 若本月不存在
            if (!monthIsExist) {
                for (Triple<String, Integer, Integer> stringDateDateTriple : monthDay) {
                    // key 第1周
                    weekGroup.put(stringDateDateTriple.getLeft(), 0);
                }
            }
            // 若存在
            if (monthIsExist) {
                List<ProcessReportMiniVO> processReportMiniVOS = timeTotalGroup.get(thisMonth);
                for (Triple<String, Integer, Integer> stringDateDateTriple : monthDay) {
                    // 取在两个时间段中间的统计值
                    // key 第1周
                    weekGroup.put(stringDateDateTriple.getLeft(), processReportMiniVOS.stream()
                            .filter(e -> e.getDay() >= stringDateDateTriple.getMiddle() && e.getDay() <= stringDateDateTriple.getRight())
                            .map(ProcessReportMiniVO::getConformityCount)
                            .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
                }
            }
            deptGroup.put("weekGroup", weekGroup);

            Map<String, BigDecimal> weekDaysCollect = processReportInfo.stream().filter(e -> weekDays.contains(e.getReportDate().substring(0, 10))).collect(Collectors.toMap(e -> e.getReportDate().substring(0, 10), ProcessReportMiniVO::getConformityCount, BigDecimal::add));
            dayGroup = Maps.newLinkedHashMap();
            for (String weekDay : weekDays) {
                // key 2020-08-03
                dayGroup.put(weekDay, weekDaysCollect.getOrDefault(weekDay, BigDecimal.ZERO));
            }
            deptGroup.put("dayGroup", dayGroup);

            allResult.add(deptGroup);
        }
        Map<String, Object> result = Maps.newHashMap();
        List<LinkedHashMap<String, Object>> linkedHashMaps = PageUtils.startPage(allResult, pageParam.getPageno(), pageParam.getPagesize());
        result.put("data", new DsResultResponse(pageParam.getPageno(), pageParam.getPagesize(), allResult.size(), linkedHashMaps));
        return R.ok(result);
    }

    @Override
    public R allDevice(DevicePageParam pageParam) {
        // 获取所有未删除的设备
        Map<String,Object> param = Maps.newHashMap();
        param.put("deptId",pageParam.getDeptId());
        List<DeviceDO> deviceDOS = deviceDao.listOfDeviceDetail(param);
        List<Long> deviceIdList = deviceDOS.stream().map(DeviceDO::getId).collect(Collectors.toList());
        param.put("deviceIdList",deviceIdList);
        if (deviceIdList.size() == 0) {
            return R.ok();
        }

        // 获取测点
        List<PointVO> pointDOS = kanbanService.getPointList(param);
        Map<Long, List<PointVO>> pointMap = pointDOS.stream().collect(Collectors.groupingBy(PointVO::getDeviceId));

        // 获取在制工单
        param.put("statusList", Arrays.asList(ConstantForMES.START_WORK, ConstantForMES.PUT_UP));
        List<AllDeviceManageVO> worKOrderList = kanbanService.getWorKOrder(param);
        Map<Long, List<AllDeviceManageVO>> workOrderMap = worKOrderList.stream().collect(Collectors.groupingBy(AllDeviceManageVO::getDeviceId));

        // 获取今日产量
        param.put("singleTime", DateFormatUtil.getFormateDate(new Date()));
        List<AllDeviceManageVO> deviceOutPut = kanbanService.getDeviceOutPut(param);
        Map<Long, Optional<BigDecimal>> outPutMap = deviceOutPut.stream()
                .collect(Collectors.groupingBy(AllDeviceManageVO::getDeviceId
                        , Collectors.mapping(AllDeviceManageVO::getOutPut, Collectors.reducing(BigDecimal::add))));
        List<AllDeviceManageVO> allDeviceManageVOList = Lists.newArrayList();
        AllDeviceManageVO allDeviceManageVO;
        for (DeviceDO deviceDO : deviceDOS) {
            Long deviceId = deviceDO.getId();
            allDeviceManageVO = new AllDeviceManageVO();

            boolean orderIsExist = workOrderMap.containsKey(deviceId);
            // 若不存在再制工单则赋停机状态
            if (!orderIsExist) {
                allDeviceManageVO.setStatusName("停机");
            }
            if (orderIsExist) {
                List<AllDeviceManageVO> allDeviceManageVOS = workOrderMap.get(deviceId);
                List<AllDeviceManageVO> startList = allDeviceManageVOS.stream().filter(e -> Objects.equals(ConstantForMES.START_WORK, e.getStatus())).collect(Collectors.toList());
                boolean isStartWork = startList.size() > 0;
                allDeviceManageVO.setStatusName(isStartWork ? "开机" : "挂起");
                if (isStartWork){
                    BeanUtils.copyProperties(startList.get(0),allDeviceManageVO);
                    allDeviceManageVO.setStatusName("开机");
                }

            }
            if (!outPutMap.containsKey(deviceId)){
                allDeviceManageVO.setOutPut(BigDecimal.ZERO);
            }else {
                Optional<BigDecimal> outPutOp = outPutMap.get(deviceId);
                BigDecimal outPut = outPutOp.orElse(BigDecimal.ZERO);
                allDeviceManageVO.setOutPut(outPut);
            }
            allDeviceManageVO.setDeviceId(deviceId);
            allDeviceManageVO.setDeviceCode(deviceDO.getSerialno());
            allDeviceManageVO.setDeviceName(deviceDO.getName());
            allDeviceManageVO.setDeviceTypeName(deviceDO.getDeviceTypeName());
            // 放入测点
            if (pointMap.containsKey(deviceId)) {
                allDeviceManageVO.setPointList(pointMap.get(deviceId));
            }
            allDeviceManageVOList.add(allDeviceManageVO);
        }
        Map<String,Object> result = Maps.newHashMap();
        List<AllDeviceManageVO> linkedHashMaps = PageUtils.startPage(allDeviceManageVOList, pageParam.getPageno(), pageParam.getPagesize());
        result.put("data", new DsResultResponse(pageParam.getPageno(), pageParam.getPagesize(), allDeviceManageVOList.size(), linkedHashMaps));

        return R.ok(result);
    }

    @Override
    public R deviceCapacityAnalysis(Long deviceId) {
        // 获取设备实际产量
        DeviceCapacityDO deviceCapacityDO = new DeviceCapacityDO();
        deviceCapacityDO.setDeviceId(deviceId);
        List<DeviceCapacityDO> materielCapacity = deviceCapacityDao.getByDeviceId(deviceCapacityDO);

        // 本周日期
        List<String> weekDays = DateUtils.getWeekDays();
        // 若设备未设置物料产能则全部为0
        if (materielCapacity.size() == 0) {
            return this.setCapacityZero(weekDays);
        }
        Map<Long, BigDecimal> materielCapacityMap = materielCapacity.stream().collect(Collectors.toMap(DeviceCapacityDO::getMaterielId, DeviceCapacityDO::getStandardCapacity));
        // 获取设备的排班计划
        Map<String, Object> param = Maps.newHashMap();
        param.put("endTimeParam", weekDays.get(6));
        // 这一周的开始时间> 排班计划的开启时间 这一周的结束时间< 排班计划的开启时间
        // 例 本周一 8.10 周日 8.16  找出包括8.16前所有的排班计划
        param.put("deviceId",deviceId);
        List<CrewSchedulingItemDO> crewSchedulingItemDOS = crewSchedulingItemDao.list(param);
        // 若无排班计划产能则全部为0
        if (crewSchedulingItemDOS.size() == 0) {
            return this.setCapacityZero(weekDays);
        }

        // 若有排班计划
        LinkedHashMap<String, Map<String, BigDecimal>> capacityList = Maps.newLinkedHashMap();
        Map<String, BigDecimal> capacityMap;
        Map<String, Object> singleParam;
        for (String weekDay : weekDays) {
            boolean isCrewSchedulingExist = false;
            // 例 本次时间 2020-08-10
            for (CrewSchedulingItemDO crewSchedulingItemDO : crewSchedulingItemDOS) {
                Date startTime = crewSchedulingItemDO.getStartTime();
                String startTimeToString = DateFormatUtil.getFormateDate(startTime, DateFormatUtil.DATE_PATTERN_YMD);
                // 排班计划 2020-08-11  > 2020-08-10 则不能使用
                if (startTimeToString.compareTo(weekDay) > 0) {
                    // 跳至下一个排班计划
                    continue;
                }
                // 排班计划 2020-08-09(10)  <= 2020-08-10 则能使用
                if (startTimeToString.compareTo(weekDay) <= 0) {
                    // 继续判断是否为停用状态
                    Integer isUse = crewSchedulingItemDO.getIsUse();
                    // 停用 且停用时间 <= 本次时间(例 停用时间2020-08-09（10）<=2020-08-10) 则不能使用
                    if (isUse == 0 && DateFormatUtil.getFormateDate(crewSchedulingItemDO.getEndTime()).compareTo(weekDay) <= 0) {
                        // 跳至下一个排班计划
                        continue;
                    }
                    isCrewSchedulingExist = true;
                    // 找出班次
                    String shiftIds = crewSchedulingItemDO.getShiftIds();
                    List<Long> shiftIdList = Lists.newArrayList(shiftIds.split(",")).stream().filter(StringUtils::isNoneEmpty).map(Long::parseLong).collect(Collectors.toList());
                    singleParam = Maps.newHashMap();
                    singleParam.put("shiftIdList", shiftIdList);
                    List<ShiftDO> shiftDOS = shiftDao.list(singleParam);
                    // 总工作时长（h）
                    BigDecimal manHour = shiftDOS.stream().map(ShiftDO::getManHour).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
                    // 应得产量 = (总工作时长(h) * 60 * 60)s / 设备产能（单件工时）s
                    // 应得产量
                    BigDecimal totalCapacity = BigDecimal.ZERO;
                    for (BigDecimal value : materielCapacityMap.values()) {
                        totalCapacity = totalCapacity.add(manHour.multiply(BigDecimal.valueOf(3600L)).divide(value, Constant.BIGDECIMAL_ZERO, BigDecimal.ROUND_HALF_UP));
                    }
                    capacityMap = Maps.newHashMap();
                    capacityMap.put("totalCapacity", totalCapacity);
                    capacityList.put(weekDay, capacityMap);
                    break;
                }
            }
            // 若不存在该天的排班计划
            if (!isCrewSchedulingExist) {
                capacityMap = Maps.newHashMap();
                capacityMap.put("rate", BigDecimal.ZERO);
                capacityMap.put("totalCapacity", BigDecimal.ZERO);
                capacityMap.put("actualCapacity", BigDecimal.ZERO);
                capacityList.put(weekDay, capacityMap);
            }
        }
        // 获取设备实际产量
        singleParam = Maps.newHashMap();
        singleParam.put("deviceId", deviceId);
        Map<String,BigDecimal> actualCapacityMap = Maps.newHashMap();
        for (String weekDay : weekDays) {
            singleParam.put("singleTime", weekDay);
            List<AllDeviceManageVO> deviceOutPut = kanbanService.getDeviceOutPut(singleParam);
            actualCapacityMap.put(weekDay,deviceOutPut.stream().map(AllDeviceManageVO::getOutPut).reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
        }

        for (Map.Entry<String, Map<String, BigDecimal>> stringMapEntry : capacityList.entrySet()) {
            Map<String, BigDecimal> value = stringMapEntry.getValue();
            // 若以计算过比例则跳出
            if (value.containsKey("rate")){
                continue;
            }
            // 若应得产量为0
            BigDecimal totalCapacity = value.get("totalCapacity");
            if (totalCapacity.compareTo(BigDecimal.ZERO)==0){
                value.put("rate", BigDecimal.ZERO);
                value.put("actualCapacity", BigDecimal.ZERO);
                continue;
            }
            // 实际产量
            BigDecimal actualCapacity = actualCapacityMap.get(stringMapEntry.getKey());
            // 产量比例 = 实际产量/应得产量
            BigDecimal rate = actualCapacity.divide(totalCapacity,2,BigDecimal.ROUND_HALF_UP);
            value.put("rate", rate.multiply(BigDecimal.valueOf(100L)));
            value.put("actualCapacity", actualCapacity);
        }
        Map<String, Object> result = Maps.newHashMap();
        result.put("data", capacityList);
        return R.ok(result);
    }

    private R setCapacityZero(List<String> weekDays) {
        Map<String, Object> result = Maps.newHashMap();
        LinkedHashMap<String, Map<String, BigDecimal>> capacityList = Maps.newLinkedHashMap();
        Map<String, BigDecimal> capacityMap;
        for (String weekDay : weekDays) {
            capacityMap = Maps.newHashMap();
            capacityMap.put("rate", BigDecimal.ZERO);
            capacityMap.put("totalCapacity", BigDecimal.ZERO);
            capacityMap.put("actualCapacity", BigDecimal.ZERO);
            capacityList.put(weekDay, capacityMap);
        }
        result.put("data", capacityList);
        return R.ok(result);
    }
}
